#include <stdio.h>
#include <stdlib.h>

int cmp(const void *a, const void *b) {
    return *(int *)a - *(int *)b;
}

// 将数组中的所有重复数字返回
int *findRepeatNumber(int* nums, int numsSize, int *return_size) {
	int i, cnt = 0, *repeat;
    qsort(nums, numsSize, sizeof(int), cmp);	//排序
	repeat = malloc(sizeof(int)*numsSize);
	for (i = 0; i < numsSize; i++) 
		repeat[i] = INT_MAX;

    for (i = 1; i < numsSize; i++) {
        if (nums[i] == nums[i-1]) {
			//repeat为空 或 nums[i] != repeat[cnt-1]
			if (cnt == 0 || \	
				(cnt > 0 && nums[i] != repeat[cnt-1])) {
				repeat[cnt++] = nums[i];
			}
		}		
    }
	repeat = realloc(repeat, cnt);
	*return_size = cnt;
    return repeat;
}


void main()
{
	int arr[] = {0,-1,-2,-2,1,1,1,2,5,2, 3, 1, 0, 2, 5, 3};
	int len = sizeof(arr)/sizeof(arr[0]);
	int count, *res;
	res = findRepeatNumber(arr, len, &count);
	printf("repeat count:%d\n",count);
	for (int i = 0; i < count; i++) 
		printf("%d ",res[i]);
}